import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@v/Layout/index.vue'
import { isLogin, getRole } from '@/utils'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'LayOut',
      component: Layout,
      children: [
        // 重定向
        {
          path: '/',
          redirect: '/DashBoard'
        },
        {
          path: '/dashBoard',
          name: '仪表盘',
          meta: {
            roles: '*'
          },
          component: () => import('@v/DashBoard/index.vue')
        },
        {
          path: '/itemLists',
          name: '商品列表',
          meta: {
            roles: ['admin', 'superAdmin'],
          },
          component: () => import('@v/ItemLists/index.vue')
        },
        {
          path: '/itemAdd',
          name: '新增商品',
          meta: {
            roles: ['admin', 'superAdmin'],
          },
          component: () => import('@v/ItemAdd/index.vue')
        },
        {
          path: '/itemUpdate',
          name: '修改商品',
          meta: {
            roles: ['admin', 'superAdmin'],
          },
          component: () => import('@v/ItemUpdate/index.vue')
        },
        {
          path: '/cateLists',
          name: '商品分类',
          meta: {
            roles: ['admin', 'superAdmin'],
          },
          component: () => import('@v/CateLists/index.vue')
        },
        {
          path: '/setting',
          name: '设置',
          meta: {
            roles: [ 'superAdmin'],
          },
          component: () => import('@v/SettingPage/index.vue')
        },
        {
          path: '/UserInfo',
          name: '用户',
          meta: {
            roles: ['superAdmin'],
          },
          component: () => import('@v/Userlnfo/index.vue')
        },
        {
          path:'ItemDraft',
          name: '商品列表草稿箱',
          meta: {
            roles: ['admin', 'superAdmin'],
          },
          component: () => import('@v/ItemDraft/index.vue')
        }
      ]
    },
    {
      path: '/login',
      name: '登录',
      meta: {
        roles: '*'
      },
      component: () => import('@v/LoginPage/index.vue')
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      meta: {
        roles: '*'
      },
      component: () => import('@v/NotFound/index.vue')
    },
    {
      path: '/noAuth',
      name: 'noAuth',
      meta: {
        roles: '*'
      },
      component: () => import('@v/NoPermission/index.vue')
    }
  ]
})

// 登录鉴权
router.beforeEach((to, from, next) => {
  const passRoutes = ['/login']
  if(passRoutes.includes(to.path)){
    next()
  } else { 
    // 非登录页面 鉴权
    if(isLogin()){
      console.log(to.meta.roles === '*' );
      console.log(to.meta.roles.includes(getRole()));
      
      if(to.meta.roles === '*' || to.meta.roles.includes(getRole())){
        // 可以访问
        next()
      }else{
        next('/noAuth')
      }
    }else{
      next('/login')
    }

  }
})

export default router
